//
//  UserInfoGroundView.m
//  ChatForHot
//
//  Created by FLY on 2018/10/14.
//  Copyright © 2018年 FLY. All rights reserved.
//

#import "UserInfoGroundView.h"
#import "UserInfoBackCell.h"
#import "XMPlayerView.h"

@interface UserInfoGroundView ()<UICollectionViewDelegate,UICollectionViewDataSource>

@end

@implementation UserInfoGroundView

- (instancetype)init{
    
    self = [super init];
    
    if (self) {
        
        [self addSubview:self.HOTCHATcollectionView];
        [self addSubview:self.HOTCHATalphaView];
        [self addSubview:self.HOTCHATnumImgView];
        [self addSubview:self.HOTCHATnumLb];
        
        [self.HOTCHATalphaView mas_updateConstraints:^(MASConstraintMaker *make) {
            
            make.edges.equalTo(self);
        }];
        
        [self.HOTCHATcollectionView mas_updateConstraints:^(MASConstraintMaker *make) {
           
            make.edges.equalTo(self);
        }];
        
        [self.HOTCHATnumImgView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self).offset(-15);
            make.height.mas_equalTo(34);
            make.bottom.equalTo(self).offset(-15-13);
            make.left.equalTo(self.HOTCHATnumLb).offset(-10);
        }];
        [self.HOTCHATnumLb mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.equalTo(self.HOTCHATnumImgView);
            make.right.equalTo(self.HOTCHATnumImgView).offset(-10);
        }];
    }
    return self;
}
- (void)setModel:(UserInfoModel *)model{
    
    _model = model;
    
    [self.HOTCHATcollectionView reloadData];
    [self scrollViewDidScroll:self.HOTCHATcollectionView];
}
#pragma mark UICollectionDatasource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.model.commonArr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    UserInfoBackCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"UserInfoBackCell" forIndexPath:indexPath];
    
    cell.model = self.model.commonArr[indexPath.row];
    
    return cell;
}
#pragma mark UICollectionview Delegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    [collectionView deselectItemAtIndexPath:indexPath animated:NO];
    
    UserInfoBackCell *cell = (UserInfoBackCell *)[collectionView cellForItemAtIndexPath:indexPath];
    if (cell.model.isVideo) {
        
        //        void (^lokkfor)(void) = ^{
        XMPlayerView *playerManager = [[XMPlayerView alloc] init];
        playerManager.sourceImagesContainerView = (UIView *)cell.HOTCHATbackImgView;
        // 当前的View
        playerManager.currentImage = cell.HOTCHATbackImgView.image;  // 当前的图片
        NSString *url = [NSString stringWithFormat:@"%@%@",APIHEADER,cell.model.url];
        playerManager.videoURL = [NSURL URLWithString:url];
        [playerManager show];
//                };
        //
        //        if (self.delegate && [self.delegate respondsToSelector:@selector(HookUplookForVideo:lookBlock:)]) {
        //            [self.delegate HookUplookForVideo:cell.model.url.HookUpmd5String lookBlock:lokkfor];
        //        }
        
        return;
    }
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(HOTCHATlookPhotoIndex:type:)]) {
        [self.delegate HOTCHATlookPhotoIndex:indexPath.row type:self.tag];
    }
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 0;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 0;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return collectionView.frame.size;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    int index = scrollView.contentOffset.x / CGRectGetWidth(self.HOTCHATcollectionView.frame);
    self.HOTCHATnumLb.text = [NSString stringWithFormat:@"%d/%zd",index + 1,self.model.commonArr.count];
    
}
- (void)setOffSetY:(CGFloat)offSetY{
    
    self.HOTCHATalphaView.alpha = offSetY / (HOTCHATUIWIDTH -20) ;
//    NSLog(@"%lf",ffff);
    [self.HOTCHATnumImgView mas_updateConstraints:^(MASConstraintMaker *make) {
        if (offSetY > 0) {
            
            make.bottom.equalTo(self).offset(-15-13-offSetY);
        }else{
            make.bottom.equalTo(self).offset(-15-13);
        }
    }];
}

//lazzy
- (UICollectionView *)HOTCHATcollectionView{
    
    {
        if (_HOTCHATcollectionView == nil) {
            UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
            [layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
            
            _HOTCHATcollectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:layout];
            _HOTCHATcollectionView.dataSource = self;
            _HOTCHATcollectionView.delegate = self;
            
            [_HOTCHATcollectionView registerClass:[UserInfoBackCell class] forCellWithReuseIdentifier:@"UserInfoBackCell"];
            
            _HOTCHATcollectionView.showsHorizontalScrollIndicator = NO;
            _HOTCHATcollectionView.backgroundColor = [UIColor clearColor];
            _HOTCHATcollectionView.pagingEnabled = YES;
            if (@available(iOS 11.0, *)) {
                _HOTCHATcollectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            } else {
                // Fallback on earlier versions
            }
        }
        return _HOTCHATcollectionView;
    }
}
- (UIView *)HOTCHATalphaView{
    if (_HOTCHATalphaView == nil) {
        _HOTCHATalphaView = [[UIView alloc] init];
        _HOTCHATalphaView.backgroundColor = [UIColor blackColor];
        _HOTCHATalphaView.alpha = 0;
        _HOTCHATalphaView.userInteractionEnabled = YES;
    }
    return _HOTCHATalphaView;
}
- (UIImageView *)HOTCHATnumImgView{
    if (_HOTCHATnumImgView == nil) {
        _HOTCHATnumImgView = [[UIImageView alloc] init];
        _HOTCHATnumImgView.image = [[UIImage imageNamed:@"picpage"] resizableImageWithCapInsets:UIEdgeInsetsMake(1, 15, 1, 15) resizingMode:(UIImageResizingModeStretch)];
    }
    return _HOTCHATnumImgView;
}
- (UILabel *)HOTCHATnumLb{
    if (_HOTCHATnumLb == nil) {
        _HOTCHATnumLb = [[UILabel alloc] init];
        _HOTCHATnumLb.font = [UIFont systemFontOfSize:12];
        _HOTCHATnumLb.textColor = [UIColor whiteColor];
        _HOTCHATnumLb.textAlignment = NSTextAlignmentCenter;
    }
    return _HOTCHATnumLb;
}

@end
